package com.ruoyi.common.utils;

import com.qunar.flight.qmonitor.QMonitor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author albon
 *         Date : 15-10-19
 *         Time: 上午10:48
 */
public class AsyncWorker {

    //配置参数
    public static final int corePoolSize = 10;
    public static final int maxPoolSize = 25;
    public static final int maxQueueSize = 500;
    //单位采用秒
    public static final int keepAliveTime = 30;



    //采用无界队列
    private static final ThreadPoolExecutor worker = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(maxQueueSize),
            new RejectedExecutionHandler() {
                public void rejectedExecution(Runnable arg0, ThreadPoolExecutor arg1) {
                    QMonitor.recordOne("async_work_task_reject");
                }
            });

    public static void execute(final Runnable runnable){
        worker.execute(runnable);
    }

    public static void shutdown(){
        if (worker != null) {
            worker.shutdownNow();
        }
    }

}
